﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using ZLCMS.Business;
using System.Collections;
using ZLCMS.Data.Model;
using NHibernate.Criterion;
using ZLCMS.Business.DwzFrame;
using System.Reflection;
using NHibernate;
using ZLCMS.Data.Common;
using ZLCMS.Business.UploadFileInfo;
using Newtonsoft.Json;

namespace ZLCMS.Web.SystemManage.Basics.Doc
{
    public partial class Edit : System.Web.UI.Page
    {
        protected FrameHelper _framePage;

        protected CmsKindInfo _kindDB;

        protected CmsDocConfig _config;

        protected void Page_Load(object sender, EventArgs e)
        {
            _framePage = new FrameHelper(this);
            if (IsPostBack)
            {
                SaveDoc();
            }
            else if (!IsPostBack)
            {
                InitializeContent();

                fc_imgurl.FileType = _config.FileType;
            }

            ddTreeControl.Kind = Request["Kind"];
        }

        protected void InitializeContent()
        {
            try
            {
                doc_id.Value = Request["uid"];

                using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    CmsDocInfo doc = session.Get<CmsDocInfo>(Int32.Parse(Request["uid"]));

                    _kindDB = session.Get<CmsKindInfo>(doc.Kind);

                    _config = session.Get<CmsDocConfig>(_kindDB.ConfigId);

                    CmsTreeDef node = null;
                    if (doc.TypeId != 0)
                    {
                        node = session.Get<CmsTreeDef>((int)doc.TypeId);
                    }
                    if (node != null)
                    {
                        ddTreeControl.SelectedNodeId = node.Id;
                        ddTreeControl.SelectedNodeText = node.Name;
                    }

                    txtTitle.Text = doc.Title;
                    txtName.Text = doc.Name;
                    txtWriter.Text = doc.Author;
                    tb_order.Text = doc.ShowOrder.ToString();

                    txtContent.Value = doc.Content;
                    txtSummary.Value = doc.Summary;

                    txtUrl.Text = doc.Url;
                    chkRcmd.Checked = DatabaseHelper.IsTrue(doc.IsRecommend);
                    chkHot.Checked = DatabaseHelper.IsTrue(doc.IsHot);
                    fc_imgurl.Value = doc.ThumbnailUrl;
                    fc_fileurl.Value = doc.AttachmentUrl;
                    tb_cdate.Text = doc.CreateDate.ToString("yyyy-MM-dd");

                    txtWebTitle.Text = doc.WebTitle;
                    txtKeyword.Text = doc.WebKeyword;
                    txtDesc.Text = doc.WebDesc;

                    try
                    {
                        UploadConfig cfg = JsonConvert.DeserializeObject<UploadConfig>(_kindDB.ExtendJasonContent);
                        fc_fileurl.Config = cfg;
                        fc_imgurl.Config = cfg;
                    }
                    catch { }
                }
            }
            catch (System.Exception ex)
            {
                _framePage.Alert(new ReturnCode() { statusCode = 300, message = "操作出错-" + ex.Message });
                return;
            }
        }

        protected void SaveDoc()
        {

            try
            {
                using(ISession session=DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
                {
                    CmsDocInfo doc = session.Get<CmsDocInfo>(Int32.Parse(doc_id.Value));

                    doc.TypeId = ddTreeControl.SelectedNodeId;
                    doc.Title = txtTitle.Text;
                    doc.Name = txtName.Text;
                    doc.Author = txtWriter.Text;

                    doc.Summary = txtSummary.Value;
                    doc.Content = txtContent.Value;

                    doc.Url = txtUrl.Text;
                    doc.ThumbnailUrl = fc_imgurl.Value;
                    doc.AttachmentUrl = fc_fileurl.Value;
                    doc.IsHot = DatabaseHelper.ConvertBool(chkHot.Checked);
                    doc.IsRecommend = DatabaseHelper.ConvertBool(chkRcmd.Checked);
                    doc.ShowOrder = int.Parse(tb_order.Text);

                    doc.WebTitle = txtWebTitle.Text;
                    doc.WebKeyword = txtKeyword.Text;
                    doc.WebDesc = txtDesc.Text;

                    doc.CreateDate = DateTime.Parse(tb_cdate.Text + " 00:00:00");
                    doc.UpdateDate = DateTime.Now;

                    session.Save(doc);
                    session.Flush();

                }
            }
            catch (System.Exception ex)
            {
                _framePage.Alert(new ReturnCode() { statusCode = 300, message = "操作出错-" + ex.Message });
            }

            ReturnCode rc = new ReturnCode()
            {
                statusCode = 200,
                message = "操作成功!"
            };

            _framePage.Alert(rc);
        }
    }
}
